home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / bplus20.zip / BPLUS.DOC < prev    next >
Text File  |  1989-03-28  |  25KB  |  599 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.                                THE B-PLUS PROGRAM
  7.                          A B-TREE INDEXING FILE MODULE
  8.                                FOR C PROGRAMMERS
  9.                                        by
  10.                              Hunter and Associates
  11.  
  12.  
  13.  
  14.               B-PLUS is a versatile, carefully designed module for C
  15.          programmers who need a fast, efficient program for indexing
  16.          data files.  B-PLUS allows data records to be retrieved based
  17.          on a key value without regard to their position in the data
  18.          file.  The data records can also be accessed in sequential
  19.          order in either a forward and reverse direction.
  20.  
  21.               The B-PLUS Program Module is based on the famous and
  22.          widely used b-tree algorithm and has a number of useful
  23.          extensions which are not found in many programs of this type.
  24.          Some of its features are the following:
  25.  
  26.               - Variable length keys are allowed
  27.  
  28.               - File size limited only by DOS or by disk space
  29.  
  30.               - All functions are non-recursive so very little stack
  31.                 space is required
  32.  
  33.               - The most recently used key values are stored in a
  34.                 cache buffer in main memory for fast access
  35.  
  36.               - Duplicate keys are allowed
  37.  
  38.               Version 1.2 of the B-PLUS program has been tested
  39.          for Microsoft C Compilers, Versions 5.0, 5.1 and the
  40.          Borland Turbo C Compiler Version 1.5 and 2.0.  The object
  41.          file is less than 10K bytes in length for these compilers.
  42.          See the instructions at the end of this user's guide for a
  43.          special note regarding Microsoft's older C Version 4.0.
  44.  
  45.               Version 2.0 has several new features that were not in
  46.          Version 1.1.  The next_key and prev_key routines can now be
  47.          called immediately after adding or deleting an index key.  It
  48.          is no longer necessary to "reset" the index file with a
  49.          find_key or locate_key function call after adding or deleting
  50.          keys.  A buffer_flush function has been added to save the 
  51.          internal buffers on the disk.
  52.  
  53.  
  54.          LICENSE AND REGISTRATION
  55.  
  56.               B-PLUS is distributed as a "share ware" program.  Please
  57.          help us get it known by giving unmodified copies of the
  58.  
  59.  
  60.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  61.          -------------------------------------------------------------
  62.  
  63.  
  64.          program and documentation to other programmers who may find
  65.          B-PLUS useful.
  66.  
  67.               B-PLUS is copyright (C) 1987 by Hunter and Associates.
  68.          It is not public domain or free software.  Non-registered
  69.          users are granted a limited license to use B-PLUS on a trial
  70.          basis for determining whether or not it is suitable for their
  71.          needs.  Registration permits the use of B-PLUS on one CPU and
  72.          allows the use of the compiled B-PLUS modules in programs for
  73.          general sale and/or distribution.
  74.  
  75.               The registration fee is $25 or $35.  Users who pay the
  76.          $35 fee will be sent a disk containing a fully commented
  77.          listing of the latest source code, the user documentation,
  78.          and a number of useful sample programs.  Users who pay the
  79.          $25 fee are not sent a new disk but are included in the
  80.          mailing list for announcements about both current and future
  81.          products.  Your prompt registration of your copy of the B-
  82.          PLUS program is appreciated.
  83.  
  84.               A trial disk with supporting documentation is available
  85.          directly from Hunter and Associates for $10.
  86.  
  87.               Register your usage of B-PLUS by sending the registra-
  88.          tion fee to:
  89.  
  90.                         Hunter and Associates
  91.                         7900 Edgewater Drive
  92.                         Wilsonville, OR  97070
  93.                         Telephone: (503) 694-1449
  94.  
  95.          Your comments regarding the B-PLUS program or any suggestions
  96.          you have for extensions or for other programs that would be
  97.          useful to you are welcomed.
  98.  
  99.               Hunter and Associates makes no warranties whatsoever
  100.          regarding the B-PLUS computer programs or the supporting
  101.          documentation.
  102.  
  103.  
  104.          USING B-PLUS IN YOUR PROGRAMS
  105.  
  106.               The B-PLUS File Indexing Module contains twelve
  107.          functions that handle the retrieval of data records by key
  108.          value.  The keys that are used to locate records are null
  109.          terminated strings.  The data structures and constants that
  110.          are used are defined in the header file bplus.h.
  111.  
  112.               If the data record field that you want to use as a key
  113.          contains numeric data, you can use one of the library
  114.  
  115.                                    Page 2
  116.  
  117.  
  118.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  119.          -------------------------------------------------------------
  120.  
  121.  
  122.          conversion routines (fcvt, evct, sprintf) to convert the data
  123.          to string format.
  124.  
  125.               The connection between a key and its reference is
  126.          formalized as a structure of type ENTRY.  This structure
  127.          contains three elements:
  128.  
  129.          typedef struct
  130.            {
  131.              RECPOS   idxptr;         /* long pointer to next index
  132.                                          level                      */
  133.              RECPOS   recptr;         /* long pointer to the file
  134.                                          position of data record    */
  135.              char     key[MAXKEY];    /* with this key value        */
  136.            } ENTRY;
  137.  
  138.               The application program uses only the recptr and key[]
  139.          fields.  The idxptr is used and maintained by the B-PLUS
  140.          modules.
  141.  
  142.               A variable of type IX_DESC is declared for each open
  143.          index file.  See the header file bplus.h if you are
  144.          interested in the elements of this structure.  ENTRY and
  145.          IX_DESC are the only two data types that are normally used by
  146.          application programs.
  147.  
  148.               Here is a sample program stub which calls the open_index
  149.          and find_index subroutines.
  150.  
  151.  
  152.          Example:
  153.  
  154.            #include "bplus.h"
  155.            main()
  156.              {
  157.                 ENTRY    e;
  158.                 IX_DESC  names;
  159.  
  160.                 /* open index file called NAMES.IDX */
  161.  
  162.                 open_index("NAMES.IDX", &names, 0);
  163.  
  164.                 /* find an index record for John Smith */
  165.  
  166.                 strcpy(e.key, "SMITH JOHN");
  167.                 if(find_key(&e, &names))
  168.                   printf("Data record address is %ld", e.recptr);
  169.                 else
  170.                   printf("Cannot find record for that key");
  171.               }
  172.  
  173.                                    Page 3
  174.  
  175.  
  176.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  177.          -------------------------------------------------------------
  178.  
  179.  
  180.          Each of the twelve subroutines is now described.
  181.  
  182.          int cdecl open_index(name, pix, dup);
  183.  
  184.               char *name;         File path name
  185.               IX_DESC *pix;       Pointer to index file variable
  186.               int dup;            0 - no duplicate keys allowed
  187.                                   1 - allow duplicate keys
  188.  
  189.               Description:  The open_index function is used to open
  190.               and initialize an existing index file specified by name
  191.               and prepares the file for subsequent reading or writing.
  192.               The file structure variable pix is defined in the
  193.               application program.  Duplicate keys are allowed or not
  194.               allowed depending on whether dup has the value of 0 or
  195.               1.  The open_index function returns the value IX_OK (1)
  196.               if the file is opened successfully.  If the file cannot
  197.               be opened, an error message is displayed and the program
  198.               is aborted.
  199.  
  200.  
  201.  
  202.          int cdecl make_index(name, pix, dup);
  203.  
  204.               char *name;         File path name
  205.               IX_DESC *pix;       Pointer to index file variable
  206.               int dup;            0 - no duplicate keys allowed
  207.                                   1 - allow duplicate keys
  208.  
  209.               Description:  The make_index function is used to create
  210.               and initialize a new index file specified by name and to
  211.               prepare the file for subsequent reading or writing.  If
  212.               a file of this name already exists, its contents are
  213.               destroyed when the new file is created.  The file
  214.               structure variable pix is defined in the application
  215.               program.  Duplicate keys are allowed or not allowed
  216.               depending on whether dup has the value of 0 or 1.  The
  217.               make_index function returns the value IX_OK (1) if the
  218.               file is created successfully.  If the file cannot be
  219.               created, an error message is displayed and the program
  220.               is aborted.
  221.  
  222.  
  223.  
  224.          int cdecl close_index(pix);
  225.  
  226.               IX_DESC *pix;       Pointer to index file variable
  227.  
  228.               Description:  The close_index file function clears the
  229.               internal cache buffer and closes the specified index
  230.  
  231.                                    Page 4
  232.  
  233.  
  234.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  235.          -------------------------------------------------------------
  236.  
  237.  
  238.               file.  It is very important that each index file be
  239.               closed.  Otherwise data that is stored in the internal
  240.  
  241.               cache buffer may be lost and the index file may not be
  242.               properly updated.  The close_index function returns the
  243.               value IX_OK (1) if the file is successfully closed.
  244.  
  245.  
  246.  
  247.          int cdecl find_key(pe, pix);
  248.  
  249.               ENTRY *pe;          Pointer to variable of type ENTRY
  250.               IX_DESC *pix;       Pointer to index file variable
  251.  
  252.               Description:  The find_key function searches the index
  253.               file for the key value contained in pe.key.  If an exact
  254.               match is found, the value IX_OK (1) is returned and the
  255.               location of the data record with this key value is
  256.               stored in pe.recptr.  If an exact match is not found,
  257.               the value IX_FAIL (0) is returned and pe.recptr is
  258.               undefined.  If the index file contains duplicate keys,
  259.               the first key is always found.
  260.  
  261.  
  262.  
  263.          int cdecl locate_key(pe, pix);
  264.  
  265.               ENTRY *pe;          Pointer to variable of type ENTRY
  266.               IX_DESC *pix;       Pointer to index file variable
  267.  
  268.               Description:  The locate key function searches the index
  269.               file for the first key value which is equal to or
  270.               greater than that stored in pe.key.  The location of the
  271.               data record which is equal to or greater than pe.key is
  272.               stored in pe.recptr.  This function can be used to
  273.               locate an entry in the index file when only part of the
  274.               key value is known.  If the index file contains
  275.               duplicate keys, locate_key will locate the first key.
  276.               The following values are returned by locate_key:
  277.  
  278.                    IX_OK  -  the value (1) is returned if an exact
  279.                              match is found
  280.  
  281.                    IX_FAIL - the value (0) is returned if an exact
  282.                              match is not found
  283.  
  284.                    EOIX  -   the value (-2) is returned for end of
  285.                              index if the search key is greater than
  286.                              all keys in the index file and pe.recptr
  287.                              is undefined.
  288.  
  289.                                    Page 5
  290.  
  291.  
  292.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  293.          -------------------------------------------------------------
  294.  
  295.  
  296.  
  297.  
  298.          int cdecl add_key(pe, pix);
  299.  
  300.               ENTRY *pe;          Pointer to variable of type ENTRY
  301.               IX_DESC *pix;       Pointer to index file variable
  302.  
  303.               Description:  The add_key function adds new entries to
  304.               the index file.  The calling program stores the key
  305.               value in pe.key and the data record address in
  306.               pe.recptr.  Add_key first looks to see if an entry with
  307.               this key already exists in the index file.  If no entry
  308.               with this key exists, the new entry is added.  If an
  309.               entry with this key already exists, the new entry is
  310.               added only if duplicate keys are allowed (as defined by
  311.               the open_index function).  If the entry is successfully
  312.               added, IX_OK (1) is returned; otherwise IX_FAIL (0) is
  313.               returned.
  314.  
  315.  
  316.  
  317.          int cdecl delete_key(pe, pix);
  318.  
  319.               ENTRY *pe;          Pointer to variable of type ENTRY
  320.               IX_DESC *pix;       Pointer to index file variable
  321.  
  322.               Description:  The delete_key function deletes entries
  323.               in the index file.  The key to be deleted is stored in
  324.               pe.key.  If duplicate records are allowed, the
  325.               corresponding data record address must also be stored in
  326.               pe.recptr.  In this case, delete key needs the record
  327.               number to distinguish entries.  If there are not
  328.               duplicate entries, this field is ignored.  If the entry
  329.               is successfully deleted, IX_OK (1) is returned;
  330.               otherwise IX_FAIL (0) is returned.  The space that was
  331.               occupied by the entry is marked as free for reused by
  332.               B_PLUS.
  333.  
  334.  
  335.  
  336.          int cdecl first_key(pix);
  337.  
  338.               IX_DESC *pix;       Pointer to index file variable
  339.  
  340.               Description:  The first_key function positions the index
  341.               file pointer to the beginning of the index file.  The
  342.               function next_key can then be used to list the file in
  343.               key order.  The first_key function returns the value
  344.               IX_OK (1).
  345.  
  346.  
  347.                                    Page 6
  348.  
  349.  
  350.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  351.          -------------------------------------------------------------
  352.  
  353.  
  354.          int cdecl last_key(pix);
  355.  
  356.               IX_DESC *pix;       Pointer to index file variable
  357.  
  358.               Description:  The last_key function positions the index
  359.               file pointer at the end of the index file.  The function
  360.               previous_key can then be used to list the file in
  361.               reverse key order.  The last_key function returns the
  362.               value IX_OK (1).
  363.  
  364.  
  365.  
  366.          int cdecl next_key(pe, pix);
  367.  
  368.               ENTRY *pe;          Pointer to variable of type ENTRY
  369.               IX_DESC *pix;       Pointer to index file variable
  370.  
  371.               Description:  The next_key function returns the next
  372.               entry in the index file.  After deleting or adding keys,
  373.               next_key returns the key immediately following the
  374.               addition or deletion.  Next_key can be used to locate
  375.               the desired data record when duplicate keys are allowed.
  376.               Next_key is used to process files sequential.  Next_key
  377.               returns the value IX_OK (1) if the next key is present
  378.               and the value EOIX (-2) when the file pointer is at the
  379.               end of the index file.  The following program processes
  380.               an indexed file sequentially:
  381.  
  382.               #include "bplus.h"
  383.               main()
  384.                 {
  385.                   ENTRY e;
  386.                   IX_DESC names;
  387.  
  388.                   /* open the index file */
  389.                   open_index("names.idx", &names);
  390.  
  391.                   /* now process the file sequentially */
  392.                   first_key(&names);
  393.                   ret = next_key(&e, &names);
  394.                   while (ret == IX_OK)
  395.                     {
  396.                       /* the data record location is e.recptr */
  397.                       /* the program would retrieve and process it */
  398.                       ret = next_key(&e, &names);
  399.                     }
  400.  
  401.                   /* remember to always close open files */
  402.                   close_index(&names);
  403.                 }
  404.  
  405.                                    Page 7
  406.  
  407.  
  408.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  409.          -------------------------------------------------------------
  410.  
  411.  
  412.          int cdecl prev_key(pe, pix);
  413.  
  414.               ENTRY *pe;          Pointer to variable of type ENTRY
  415.               IX_DESC *pix;       Pointer to index file variable
  416.  
  417.               Description:  The prev_key function returns the previous
  418.               entry in the index file.  After deleting or adding keys,
  419.               prev_key returns the key immediately preceeding the
  420.               addition or deletion.  Prev_key can be used to process
  421.               files sequentially in reverse order. Prev_key returns
  422.               the value IX_OK (1) if there is a previous key and the
  423.               value EOIX (-2) when the file pointer is at the
  424.               beginning of the index file.
  425.  
  426.  
  427.  
  428.          int cdecl find_exact(pe, pix);
  429.  
  430.               ENTRY *pe;          Pointer to variable of type ENTRY
  431.               IX_DESC *pix;       Pointer to index file variable
  432.  
  433.               Description:  The find_exact function searches the index
  434.               file for the key value contained in pe.key and the data
  435.               record position stored in pe.recptr.  If an exact match
  436.               is found for both of these values, the value IX_OK (1)
  437.               is returned, and the internal index file pointer is set
  438.               to that position in the index file.  If an exact match
  439.               is not found, the value IX_FAIL (0) is returned.
  440.  
  441.  
  442.  
  443.          int cdecl buffer_flush(pix);
  444.          
  445.               IX_DESC * pix;     Pointer to index file variable
  446.               
  447.               Description:  The buffer_flush function writes any of
  448.               the internal working buffers that have data which has
  449.               been change to the disk.  This function can be called
  450.               after adding or deleting keys to insure that the disk
  451.               files are immediately updated. 
  452.  
  453.               
  454.          TAILORING OR CHANGING B-PLUS
  455.  
  456.               Most applications can use the B-PLUS program as it is
  457.          distributed by Hunter and Associates without any changes.  It
  458.          allows multiple, large data files to be indexed in a fast,
  459.          efficient manner.  However, a description of the values that
  460.          can be changed to tailor B-PLUS are given in this section.
  461.  
  462.               An index tree becomes full when no more entries can be
  463.          added to the tree.  This is the case when adding another
  464.  
  465.                                    Page 8
  466.  
  467.  
  468.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  469.          -------------------------------------------------------------
  470.  
  471.  
  472.          entry to the tree would cause the tree to grow larger than
  473.          its maximum allowed height.  This height depends on the size
  474.          of the index blocks and the average size of the keys used by
  475.          the data files.  The minimum capacity of a b-tree index is
  476.          given by the following formula:
  477.  
  478.               C = (B / E + 1) * (B / (2 * E)  + 1) ** (H - 1)
  479.  
  480.          where C is the number of entries in the index file, B is the
  481.          block size in bytes, E is the average size of an ENTRY in
  482.          bytes, and H is the maximum tree height.
  483.  
  484.               The maximum key size is defined by MAXKEY and is set at
  485.          100.  The block size is 1024 bytes as defined by IXB_SIZE.
  486.          Ten bytes are used by pointers so 1014 bytes are used by
  487.          entries.  The size of an ENTRY is 9 + the key length.
  488.  
  489.               Thus, if the average key length is 11, an average ENTRY
  490.          is 20 bytes long and the minimum number of entries that can
  491.          be contained in a tree of height 4 is:
  492.  
  493.               C = (1014 / 20 + 1) * (1014 / 40 + 1) ** 3
  494.                 = 945,072
  495.  
  496.          If the average ENTRY is 40 bytes long, the minimum number of
  497.          entries that can be contained in a tree of height 4 is
  498.          67,384.  The corresponding values for a tree of height 3 are
  499.          35,896 and 4927, respectively.
  500.  
  501.               The maximum tree height is determined by MAX_LEVELS and
  502.          is set to eight.  Very little memory space is used by
  503.          allowing the maximum tree height to be this large.  B-PLUS
  504.          increases the height of the tree dynamically as required by
  505.          the number of records in the data file.
  506.  
  507.               If your application does not use long keys and your
  508.          files are not huge, IXB_SIZE can be changed to 512 bytes with
  509.          only a slight degradation in performance.
  510.  
  511.               The root of an open index file is always memory resident
  512.          as defined by the variable of type IX_DESC.  A dynamic pool
  513.          of cache buffers is used for other index blocks of the tree.
  514.          The number of blocks in the pool is defined by NUM_BUFS with
  515.          a default value of 8.  Each memory block is sizeof(IXB_SIZE)
  516.          + 8 bytes in length so approximately 8K of memory is used for
  517.          cache storage of index blocks.  If the number of index files
  518.          that are open simultaneously is larger than 4, you may want
  519.          to increase NUM_BUFS.  If the usage of memory is critical,
  520.          the number of buffers can be decreased.  However, NUM_BUFS
  521.          must be at least 2.  In general, the speed of file access can
  522.  
  523.  
  524.                                    Page 9
  525.  
  526.  
  527.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  528.          -------------------------------------------------------------
  529.  
  530.          be expected to improve if the number of buffers is increased
  531.          since more of the index file is memory resident.
  532.  
  533.               Because some indices are always memory resident, and
  534.          because the DOS Operating System requires it, it is very
  535.          important that all open index files be closed before an
  536.          application program terminates.
  537.  
  538.               As stated earlier, the B-PLUS program has been tested
  539.          using Microsoft's Optimizing C Compilers, Versions 4, 4.5 and
  540.          5.0, and Borland's Turbo C, Version 1.0.  However, standard K
  541.          & R programming guidelines are followed so B-PLUS should be
  542.          able to be used with other C Compilers with little
  543.          modification.  Since B-PLUS is non-recursive, the usage of
  544.          stack space does not change dynamically.  It is recommend
  545.          that the B-PLUS program be complied without stack checking.
  546.          For Microsoft C, the /Ox option can be used to maximize speed
  547.          and minimize code size.  For Turbo C, B-PLUS can be complied
  548.          with maximum optimization to minimize the object size and
  549.          improve performance.
  550.  
  551.  
  552.          ACKNOWLEDGMENTS AND REFERENCES
  553.  
  554.               The following reference materials were used and helpful
  555.          in writing the B-PLUS program:
  556.  
  557.               Wirth, Niklaus:
  558.                      Algorithms + Data Structures = Programs
  559.                      Prentice Hall (1976)
  560.  
  561.               Hunt, William James:
  562.                      The C Toolbox
  563.                      (Serious C Programming for the IBM PC)
  564.                      Addison-Wesley (1985)
  565.  
  566.  
  567.               Wirth's book is a standard reference source for binary
  568.          trees and data structures.  Hunt's C Toolbox contains useful
  569.          C programming concepts with carefully constructed programming
  570.          examples.
  571.  
  572.  
  573.          USING THE BPLUS ROUTINES
  574.  
  575.               The BPLUS.C routines must be compiled and the object
  576.          file (BPLUS.OBJ) loaded with programs that use the B_PLUS
  577.          toolkit.  Several sample C programs have been included which
  578.          illustrate how to use the BPLUS Toolkit.  These programs
  579.          should be compiled and run to make sure your copy of the
  580.          program is correct.
  581.  
  582.  
  583.                                   Page 10
  584.  
  585.  
  586.  
  587.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  588.          -------------------------------------------------------------
  589.  
  590.  
  591.          A SPECIAL NOTE REGARDING MICROSOFT C 4.0 COMPILER
  592.  
  593.               The Microsoft C library routines are different for
  594.          Version 4.0 and for Version 5.0 and Quick C.  In particular,
  595.          the memmove routine must be changed to the memcpy routine in
  596.          Version 4.0.  A macro is included in BPLUS.C which makes this
  597.          substitution.  Remove the comments delimiters for this version.
  598.  
  599.